﻿$(function () {

    // button
    $("a").button();

    if ($("#home-page").size()) {

    } else if ($("#register-page").size()) {
        $("#register").click(function () {
            var user = $("#user").val();
            var pwd = $("#pwd").val();
            var rptpwd = $("#rptpwd").val();
            var cerr = $("#cerr");
            var cout = $("#cout");
            var button = $(this);

            // clear
            cerr.hide();
            cout.hide();

            if (!user || !pwd) {
                cerr.html("请输入用户名和密码！").fadeIn();
                return;
            }
            if (pwd != rptpwd) {
                cerr.html("重复密码不一致！").fadeIn();
                return;
            }

            $.ajax({
                type: "POST",
                url: "api/register.php",
                contentType: "application/json",
                data: JSON.stringify({
                    "user": user,
                    "pwd": pwd
                }),
                beforeSend: function () {
                    button.attr("disabled", "disabled");
                },
                error: function (xhr, text, err) {
                    if (xhr.status == 500) {
                        cerr.html(JSON.parse(xhr.responseText).error).fadeIn();
                    } else {
                        cerr.html(xhr.statusText).fadeIn();
                    }
                },
                success: function () {
                    cout.html("注册成功，请牢记用户名密码，祝游戏愉快").fadeIn();
                },
                complete: function () {
                    button.removeAttr("disabled");
                }
            });
        });
    } else if ($("#change-password-page").size()) {
        $("#change-password").click(function () {
            var user = $("#user").val();
            var pwd = $("#pwd").val();
            var newpwd = $("#newpwd").val();
            var rptpwd = $("#rptpwd").val();
            var cerr = $("#cerr");
            var cout = $("#cout");
            var button = $(this);

            // clear
            cerr.hide();
            cout.hide();

            if (!user || !pwd || !newpwd) {
                cerr.html("请输入用户名和密码！").fadeIn();
                return;
            }
            if (newpwd != rptpwd) {
                cerr.html("重复密码不一致！").fadeIn();
                return;
            }

            $.ajax({
                type: "POST",
                url: "api/change-password.php",
                contentType: "application/json",
                data: JSON.stringify({
                    "user": user,
                    "pwd": pwd,
                    "newpwd": newpwd
                }),
                beforeSend: function () {
                    button.attr("disabled", "disabled");
                },
                error: function (xhr, text, err) {
                    if (xhr.status == 500) {
                        cerr.html(JSON.parse(xhr.responseText).error).fadeIn();
                    } else {
                        cerr.html(xhr.statusText).fadeIn();
                    }
                },
                success: function () {
                    cout.html("修改密码成功，请牢记新密码，祝游戏愉快").fadeIn();
                },
                complete: function () {
                    button.removeAttr("disabled");
                }
            });
        });
    }
});